package com.mrd.gtimp.business.action;

import com.alibaba.fastjson.JSONArray;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.business.entity.GoodsCode;
import com.mrd.gtimp.business.service.GoodsCodeBiz;
import com.mrd.util.HttpClientQPTool;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author yangyl
 */

@Controller
@RequestMapping("/business")
public class GoodsCodeAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private GoodsCodeBiz goodsCodeBiz;

    /**
     * 公开查询，参数t=code_t，返回所有code_t+code_s
     */
    @RequestMapping("/goodscode-q")
    @ResponseBody
    public String q() {
        Result msg;
        Filter filters = null;
        HttpUtils.getRequest().setAttribute("callback", HttpUtils.getParam("callback"));
        try {
            String k = HttpUtils.getParam("t");
            if (StringUtils.isNotEmpty(k)) {
                k = k.trim();
            }
            filters = new Filter();
            filters.addRule(new SearchRule("CODE_T", SearchRuleOption.eq, k));
//            codes = goodsCodeBiz.findByJqgrid(1, 50, filters, null);
            Pageable page = new PageRequest(1, 50, Sort.Direction.DESC);
            Jqgrid jqgrid = new Jqgrid(filters, page);
            Page<GoodsCode> codes = goodsCodeBiz.findByJqgrid(jqgrid);
            msg = Result.newSuccessInstance(JSONArray.toJSONString(codes));
        } catch (QueryException e) {
            msg = Result.newFailInstance("[]");
        }
        return "/msgText";
    }

    /**
     * 商品编号查询
     */
    @RequestMapping("/goodscode-findGoodsCode")
    @ResponseBody
    public Page<GoodsCode> findGoodsCode(@RequestParam(required = false) String k) {
        try {
            return goodsCodeBiz.findGoodOfCode(k);
//            String k = HttpUtils.getParam("k");
////            String tdateaa = HttpUtils.getParam("tdateaa");
//            if (StringUtils.isNotEmpty(k)) {
//                k = k.trim();
//            }
//            jqgrid.addRule(new SearchRule("CODE_T", SearchRuleOption.cn, k));
//            Page<GoodsCode> codes = goodsCodeBiz.findByJqgrid(jqgrid);
//            if (codes.getSize() > 0) {
//                return codes;
//            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从网上获取要素
     */
    @RequestMapping("/goodscode-getGoodsMode")
    @ResponseBody
    public Result getGoodsMode(String code_t, String code_s) {
        Result msg = null;
        try {
            String resultString = "";
            // System.out.println("get from net");
            String goodsCode = code_t + (code_s.equals("N") ? "00" : code_s);
            // resultString = HttpClient.getGoodsModeFromNet(goodsCode);
            resultString = HttpClientQPTool.getGoodsModeFromNetByJsoup(goodsCode);
            if ("".equals(resultString)) {
                resultString = "税号：" + goodsCode + "，无法从海关网站获取到要素";
                msg = Result.newSuccessInstance("{\"state\":0,\"msg\":\"" + resultString + "\"}");
            } else {
                updateGoodsMode(code_t, code_s, resultString);
                msg = Result.newFailInstance("{\"state\":1,\"msg\":\"" + resultString + "\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 更新从网上获取的要素
     */
    @RequestMapping("/goodscode-updateGoodsMode")
    private void updateGoodsMode(String code_t, String code_s, String resultString) {
        try {
            String hql = "update com.mrd.gtimp.business.pojo.GoodsCode gc set gc.GOODS_MODE = '"
                    + resultString + "' where gc.CODE_T='" + code_t + "' and gc.CODE_S='" + code_s
                    + "'";
            goodsCodeBiz.updateByHql(hql);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 商品规格查询
     */
    @RequestMapping("/goodscode-findGoodsMode")
    @ResponseBody
    public Page<GoodsCode> findGoodsMode() {
        Filter filters = null;
        try {
            String code_t = HttpUtils.getParam("code_t");
            String code_s = HttpUtils.getParam("code_s");
            filters = new Filter();
            filters.addRule(new SearchRule("CODE_T", SearchRuleOption.eq, code_t));
            filters.addRule(new SearchRule("CODE_S", SearchRuleOption.eq, code_s));
//            codes = goodsCodeBiz.findByJqgrid(1, 5, filters, null);
            Pageable page = new PageRequest(1, 5, Sort.Direction.DESC);
            Jqgrid jqgrid = new Jqgrid(filters, page);
            Page<GoodsCode> codes = goodsCodeBiz.findByJqgrid(jqgrid);
            return codes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Resource
    public void setGoodsCodeBiz(GoodsCodeBiz goodsCodeBiz) {
        this.goodsCodeBiz = goodsCodeBiz;
    }

}
